package com.itlong.cloud.operate.service.impl;

import com.itlong.cloud.POJO.DTO.operate.OperateParkingLotManageInfoDTO;
import com.itlong.cloud.POJO.DTO.operate.OperateParkingLotManagePageDTO;
import com.itlong.cloud.POJO.PO.OperateParkManagePO;
import com.itlong.cloud.POJO.VO.operate.OperateParkingLotManageInfoVO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.constants.RedisConstant;
import com.itlong.cloud.enumerate.DataStatusEnum;
import com.itlong.cloud.operate.dao.IOperateParkingLotManageDao;
import com.itlong.cloud.operate.service.IOperateParkingLotManageService;
import com.itlong.cloud.service.RedisService;
import com.itlong.cloud.utils.data.handler.SqlUtil;
import com.itlong.cloud.utils.page.Page;
import com.itlong.cloud.utils.random.LogicIdUtil;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <desc>
 *      运营-项目-人脸应用服务接口实现。
 * <desc/>
 *
 * @createDate 2018-05-03.
 */
@Service
public class OperateParkingLotManageServiceImpl implements IOperateParkingLotManageService {

    private static final Logger LOG = LoggerFactory.getLogger(OperateParkingLotManageServiceImpl.class);


    @Autowired
    IOperateParkingLotManageDao iOperateParkingLotManageDao;

    @Autowired
    RedisService redisService;

    /**
     * <desc>
     *      停车场信息分页查询
     * <desc/>
     *
     * @param  operateParkingLotManagePageDTO 分页查询参数
     * @return
     * @author Qiang.S
     * @createDate 2018-11-05
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.NOT_SUPPORTED)
    public Page<OperateParkingLotManageInfoVO> getParkingLotPage(OperateParkingLotManagePageDTO operateParkingLotManagePageDTO) throws Exception{
        //将DTO转换为Map params
        Map<String, Object> params = SqlUtil.convertBeanToMapParams(operateParkingLotManagePageDTO);
        //分页类默认pageSize为20
        Page<OperateParkingLotManageInfoVO> page = new Page<>();
        page.getPage().setPageSize(operateParkingLotManagePageDTO.getPageSize());
        page.getPage().setCurrentPage(operateParkingLotManagePageDTO.getCurrentPage());
        params.put("currentPage",page.getPage().getStartSlip());
        params.put("pageSize",page.getPage().getPageSize());
        List<OperateParkingLotManageInfoVO> list =  iOperateParkingLotManageDao.getParkingLotPage(params);
        list.forEach(operateParkingLotManageInfoVO->{
            LOG.info(RedisConstant.PARKING_IS_ONLINE_KEY+operateParkingLotManageInfoVO.getProjectId()+" 停车场信息:"+redisService.get(RedisConstant.PARKING_IS_ONLINE_KEY+operateParkingLotManageInfoVO.getProjectId()));
            if (redisService.get(RedisConstant.PARKING_IS_ONLINE_KEY+operateParkingLotManageInfoVO.getProjectId())!=null) {
                operateParkingLotManageInfoVO.setOnlineStatus("1");
            }else {
                operateParkingLotManageInfoVO.setOnlineStatus("2");
            }
        });
        page.setRows(list,iOperateParkingLotManageDao.getParkingLotCount(params));
        return page;
    }

    /**
     * <desc>
     *      新增停车场
     * <desc/>
     *
     * @param operateParkingLotManageInfoDTO 停车场信息
     * @return
     * @author Qiang.S
     * @createDate 2018-11-05
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.NOT_SUPPORTED)
    public Integer save(OperateParkingLotManageInfoDTO operateParkingLotManageInfoDTO) throws Exception{
        Date nowTime = new Date();
        OperateParkManagePO operateParkManagePO = new OperateParkManagePO();
        if ("".equals(operateParkingLotManageInfoDTO.getParkingSpaceTotal())) {
            operateParkingLotManageInfoDTO.setParkingSpaceTotal("0");
        }
        BeanUtils.copyProperties(operateParkingLotManageInfoDTO,operateParkManagePO);
        operateParkManagePO.setParkId(LogicIdUtil.bussinessId());
        operateParkManagePO.setProjectKey(UUID.randomUUID().toString().replace("-", ""));
        operateParkManagePO.setOnlineStatus("2");
        operateParkManagePO.setStatus(DataStatusEnum.NORMAL.getType());
        operateParkManagePO.setCreateTime(nowTime);
        return iOperateParkingLotManageDao.save(SqlUtil.durableData(operateParkManagePO, PlatformConstants.TABLE_SAVE));
    }

    /**
     * <desc>
     *      更新停车场
     * <desc/>
     *
     * @param operateParkingLotManageInfoDTO 停车场信息
     * @return
     * @author Qiang.S
     * @createDate 2018-11-05
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.NOT_SUPPORTED)
    public Integer update(OperateParkingLotManageInfoDTO operateParkingLotManageInfoDTO) throws Exception{
        Date nowTime = new Date();
        OperateParkManagePO operateParkManagePO = new OperateParkManagePO();
        if ("".equals(operateParkingLotManageInfoDTO.getParkingSpaceTotal())) {
            operateParkingLotManageInfoDTO.setParkingSpaceTotal("0");
        }
        BeanUtils.copyProperties(operateParkingLotManageInfoDTO,operateParkManagePO);
        operateParkManagePO.setUpdateTime(nowTime);
        operateParkManagePO.setPlanId(operateParkingLotManageInfoDTO.getPlanId());
        operateParkManagePO.setProjectId(null);
        return iOperateParkingLotManageDao.update(SqlUtil.durableData(operateParkManagePO, PlatformConstants.TABLE_UPDATE));
    }

    /**
     * <desc>
     *      判断停车场是否被使用
     * <desc/>
     *
     * @param parkId 停车场编号
     * @return
     * @author Qiang.S
     * @createDate 2018-11-05
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.NOT_SUPPORTED)
    public Integer checkParkingLot(String parkId) throws Exception{
        return iOperateParkingLotManageDao.checkParkingLot(parkId);
    }

    /**
     * <desc>
     *      判断该项目是否已有停车场
     * <desc/>
     *
     * @param projectId 项目编号
     * @return
     * @author Qiang.S
     * @createDate 2018-11-05
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.NOT_SUPPORTED)
    public Integer checkParkingRepeat(String projectId) throws Exception{
        return iOperateParkingLotManageDao.checkParkingRepeat(projectId);
    }

    /**
     * <desc>
     *      删除停车场
     * <desc/>
     *
     * @param parkId 停车场编号
     * @return
     * @author Qiang.S
     * @createDate 2018-11-05
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.NOT_SUPPORTED)
    public Integer delete(String parkId) throws Exception{
        return iOperateParkingLotManageDao.delete(parkId);
    }

    /**
     * <desc>
     *      根据停车场编号获取停车场信息
     * <desc/>
     *
     * @param parkId 停车场编号
     * @return
     * @author Qiang.S
     * @createDate 2018-11-05
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.NOT_SUPPORTED)
    public OperateParkingLotManageInfoVO getDetail(String parkId) throws Exception{
        return iOperateParkingLotManageDao.getDetail(parkId);
    }

}
